<script>
	export default {
		globalData: {
			isLogin: false,
			userInfo: null
		},
		onLaunch: function() {
			console.log('App Launch')
			this.checkLoginStatus()
			
			// 获取系统信息
			this.getSystemInfo()
		},
		onShow: function() {
			console.log('App Show')
			this.checkLoginStatus()
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			// 获取系统信息
			getSystemInfo() {
				try {
					const systemInfo = uni.getSystemInfoSync()
					console.log('当前系统信息:', systemInfo)
					
					// 保存平台信息
					uni.setStorageSync('platform', systemInfo.platform)
					uni.setStorageSync('system_info', systemInfo)
					
					// 检测是否为鸿蒙系统
					const isHarmonyOS = systemInfo.system && 
						(systemInfo.system.toLowerCase().includes('harmonyos') ||
						(systemInfo.system.toLowerCase().includes('harmony')))
					
					if (isHarmonyOS) {
						console.log('检测到鸿蒙系统')
						uni.setStorageSync('is_harmony_os', true)
					}
				} catch (e) {
					console.error('获取系统信息失败:', e)
				}
			},
			
			updateLoginStatus(isLogin, userInfo = null) {
				console.log('更新登录状态:', isLogin, userInfo)
				
				// 更新全局数据
				const globalData = getApp().$vm.$globalData || getApp().globalData
				if (globalData) {
					globalData.isLogin = isLogin
					if (userInfo) {
						globalData.userInfo = userInfo
						uni.setStorageSync('userInfo', userInfo)
					}
				}
				
				if (isLogin) {
					uni.setStorageSync('loginState', 'logged_in')
				} else {
					uni.removeStorageSync('loginState')
					uni.removeStorageSync('userInfo')
					if (globalData) {
						globalData.userInfo = null
					}
				}
			},
			
			async checkLoginStatus() {
				try {
					const loginState = uni.getStorageSync('loginState')
					console.log('检查登录状态:', loginState)
					
					// 检查全局数据对象
					const globalData = getApp().$vm.$globalData || getApp().globalData
					
					if (loginState === 'logged_in') {
						const userInfo = uni.getStorageSync('userInfo')
						if (userInfo && userInfo.id) {
							if (globalData) {
								globalData.isLogin = true
								globalData.userInfo = userInfo
							}
							return true
						}
					}
					
					const accessToken = uni.getStorageSync('accessToken')
					const userInfo = uni.getStorageSync('userInfo')
					
					if (accessToken && userInfo && userInfo.id) {
						if (globalData) {
							globalData.isLogin = true
							globalData.userInfo = userInfo
						}
						uni.setStorageSync('loginState', 'logged_in')
						return true
					}
					
					this.clearLoginState()
					return false
				} catch (error) {
					console.error('检查登录状态失败:', error)
					this.clearLoginState()
					return false
				}
			},
			
			clearLoginState() {
				console.log('清除登录状态')
				
				// 更新全局数据
				const globalData = getApp().$vm.$globalData || getApp().globalData
				if (globalData) {
					globalData.isLogin = false
					globalData.userInfo = null
				}
				
				// 清除存储
				uni.removeStorageSync('loginState')
				uni.removeStorageSync('accessToken')
				uni.removeStorageSync('refreshToken')
				uni.removeStorageSync('userInfo')
			}
		}
	}
</script>

<style>
	/*每个页面公共css */
	page {
		font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', Helvetica, 
			Segoe UI, Arial, Roboto, 'PingFang SC', 'miui', 'Hiragino Sans GB', 
			'Microsoft Yahei', sans-serif;
		font-size: 14px;
		color: #333;
		background-color: #f8f8f8;
	}

	/* 通用样式 */
	.container {
		width: 100%;
		min-height: 100vh;
		display: flex;
		flex-direction: column;
	}

	/* 适配暗黑模式 */
	@media (prefers-color-scheme: dark) {
		page {
			background-color: #222;
			color: #f0f0f0;
		}
	}

	/* 适配小程序 */
	/* #ifdef MP-WEIXIN */
	page {
		line-height: 1.6;
	}
	/* #endif */

	/* 适配鸿蒙OS */
	/* #ifdef APP-PLUS */
	page {
		--harmony-primary: #007DFF;
		--harmony-secondary: #FA8919;
	}
	/* #endif */
</style> 
